﻿var apiAddress = "http://localhost:2028/api/";

function callAjaxPostQuery(request, controller, successFunction) {
    $.ajax({
        type: "POST",
        url: apiAddress + controller,
        crossDomain: true,
        data: JSON.stringify(request),
        dataType: "json",
        success: function (data) {
            successFunction(data);
        }
    });
}

/*--------------------------------------------LOGIN-------------------------------------------------------*/

function authorizeUser(login, password) {
    var request = {
        "Login": login,
        "Password": password
    };
    callAjaxPostQuery(request, 'Login/AuthorizeUser', successAuthorization);
}

/*--------------------------------------------USERS-------------------------------------------------------*/

function getAllMyTeams(userId) {
    $.ajax({
        type: "GET",
        url: apiAddress + 'Team/GetAllUserTeams?userId='+userId,
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            successShowMyTeams(data);
        }
    });
}

function getAllTeams(userId) {
    $.ajax({
        type: "GET",
        url: apiAddress + 'Team/GetAllTeams?userId=' + userId,
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            successShowMyTeams(data);
        }
    });
}

function getAllUsers() {
    $.ajax({
        type: "GET",
        url: apiAddress + 'User/GetAllUsers',
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            successShowUsers(data);
        }
    });
}

function addUser(name, sname, phone, email, login, password, status, userId) {
    var request = {
        "Name": name,
        "Surname": sname,
        "Phone": phone,
        "Email": email,
        "Login": login,
        "Password": password,
        "Status": status,
        "UserId": userId
    };
    callAjaxPostQuery(request, 'User/RegistrateNewUser', successAddUser);
}

function editUser(userId, userIdToEdit, name, surname, phone, email, password, status) {
    var request = {
        "UserId": userId,
        "UserIdToEdit": userIdToEdit,
        "Name": name,
        "Surname": surname,
        "Phone": phone,
        "Email": email,
        "Password": password,
        "Status": status
    };
    callAjaxPostQuery(request, 'User/EditUser', successEditUser);
}

function removeUser(userId, userIdToRemove) {
    var request = {
        "UserId": userId,
        "UserIdToRemove": userIdToRemove
    };

    $.ajax({
        type: "POST",
        url: apiAddress + 'User/RemoveUser',
        crossDomain: true,
        data: JSON.stringify(request),
        dataType: "json",
        success: function (data) {
            successRemoveUser(data, userIdToRemove);
        }
    });
}

/*--------------------------------------------HOME PAGE-------------------------------------------------------*/

function initMyHomePage(userId) {
    $.ajax({
        type: "GET",
        url: apiAddress + 'MainPage/GetMainPageInfo?UserId=' + userId,
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            initPageData(data, false);
        }
    });
}

function initAllHomePage(userId) {
    $.ajax({
        type: "GET",
        url: apiAddress + 'MainPage/GetMainPageInfoAdmin?UserId=' + userId,
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            initPageData(data, true);
        }
    });
}

/*--------------------------------------------TASKS-------------------------------------------------------*/

function initMyTasks(userId) {
    $.ajax({
        type: "GET",
        url: apiAddress + 'Task/GetAllUserTasks?UserId=' + userId,
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            successShowUserTasks(data);
        }
    });
}

function initAllTasksFromGoals(userId) {
    $.ajax({
        type: "GET",
        url: apiAddress + 'Task/GetAllTasksForUserBasedOnGoals?UserId=' + userId,
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            successShowAllTasks(data);
        }
    });
}

function initMyDoneTasks(userId) {
    $.ajax({
        type: "GET",
        url: apiAddress + 'Task/GetAllUserTasks?UserId=' + userId,
        crossDomain: true,
        dataType: "json",
        success: function (data) {
            successShowuserDoneTasks(data);
        }
    });
}